package reflect;

import java.lang.reflect.Method;

/**
 * GetMethod
 *
 * @author nanxin.chen
 * @date 2015/9/28
 */
public class GetMethod {
    private void priMethod(){
        System.out.println("this is a private method.");
    }

    public void pubMethod(int a, int b) {
        System.out.println("this is a public method. And result of a+b is:" + (a + b));
    }

    public static void main(String[] args) throws Exception {
        Class c = GetMethod.class;
        Method m[] = c.getMethods();
        Method dM[] = c.getDeclaredMethods();
        System.out.println("------get public methods:");
        for (Method mm : m) {
            System.out.println(mm.getName());
        }
        System.out.println("------get declared methods(no parent's method):");
        for (Method mm : dM) {
            System.out.println(mm.getName());
        }
        //调用private方法
        for (Method mm : dM) {
            if (mm.getName().equals("priMethod")) {
                try {
                    mm.invoke(c.newInstance(),null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println(mm.getName());
        }
        System.out.println("========================================");
        c.getDeclaredMethod("pubMethod", int.class, int.class).invoke(c.newInstance(),1,1);
    }
}
